******SET YOUR DIRECTORY to ...\replication


clear all
set more off

use "finaldata\final_cc_data.dta", clear

drop if fipsplace_00==.
xtset fipsplace_00 year

drop if year>1991

*Create instrument and outcome
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
gen inter_badtetra=(1-good_soil)*tetra_corr
g perc_cc =  tot_cc_oripop_corr / (tot_ncc_oripop_corr + tot_cc_oripop_corr)

*Generate 1960 and 1947 variables
foreach var of varlist cpctblk pctblk{
gen `var'_1960_2=`var' if year==1960
egen `var'_1960=max(`var'_1960_2), by(FMSA)
drop `var'_1960_2
}
gen prop_black_1960=cpctblk_1960/pctblk_1960
egen rays_planc_1947=max(rays_planc), by(fipsplace_00)

*Create heterogeneities
gen inter_rays_planc_1947=inter_badtetra*rays_planc_1947
gen inter_cpctblk_1960=inter_badtetra*cpctblk_1960
gen inter_prop_black_1960=inter_badtetra*prop_black_1960

*Cluster variables
egen cdivcodeyear=group(cdivcode year)
 
*Label variables
label var rays_planc "Rays in plan"
label var inter_badtetra "High LB x Lead"
label var inter_cpctblk_1960 "High LB x Lead x Blacks CC 1960"
label var inter_prop_black_1960 "High LB x Lead x Blacks CC / MSA 1960"
label var inter_rays_planc_1947 "High LB x Lead x Planned highways CC"
label var cdivcodeyear "Census Division Code x Year"


reghdfe perc_cc inter_badtetra inter_prop_black_1960, abs(fipsplace_00 year cdivcode#year) vce(cluster cdivcode#year)
g SAMPLE = (e(sample)==1)

***RF
reghdfe perc_cc inter_badtetra, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
outreg2 using table2, tex(frag) nocon noni nor2 nonotes label ctitle("Sh. Pop CC") replace
***RF
reghdfe perc_cc inter_badtetra if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
outreg2 using table2, tex(frag) nocon noni nor2 nonotes label ctitle("Sh. Pop CC") 
***Interaction with blacks CC
reghdfe perc_cc inter_badtetra inter_cpctblk_1960 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
outreg2 using table2, tex(frag) nocon noni nor2 nonotes label ctitle("Sh. Pop CC") 
***Interaction with blacks CC / Blacks MSA
reghdfe perc_cc inter_badtetra inter_prop_black_1960 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
outreg2 using table2, tex(frag) nocon noni nor2 nonotes label ctitle("Sh. Pop CC") 
***Interaction with highways CC
reghdfe perc_cc inter_badtetra inter_rays_planc_1947 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
outreg2 using table2, tex(frag) nocon noni nor2 nonotes label ctitle("Sh. Pop CC") 
***Interaction with all
reghdfe perc_cc inter_badtetra inter_cpctblk_1960 inter_rays_planc_1947 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
outreg2 using table2, tex(frag) nocon noni nor2 nonotes label ctitle("Sh. Pop CC") 
